//
//  DrawSourceVC.m
//  Cat
//
//  Created by dllo on 15/12/1.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DrawSourceVC.h"

@interface DrawSourceVC ()

@property (nonatomic, assign) NSInteger index;
@property (nonatomic, copy) NSString *ver;
@property (nonatomic, assign) NSInteger pnum;

@end

@implementation DrawSourceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self refreshAction];
    [self.seg addTarget:self action:@selector(whichPage:) forControlEvents:UIControlEventValueChanged];
    [self createSegm:3];
    
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.tableView.header = header;
    
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.tableView.footer = footer;
    
    self.pnum = 1;
}

- (void)refreshAction {
    if ([self.tableView.footer isRefreshing]) {
        self.pnum++;
        NSString *URL;
        if (self.index == 1) {
            URL = [NSString stringWithFormat:@"http://api.huapipi.com/painting/original?deviceId=1826162&pnum=%ld&uid=1033268&entrance=hot&ver=%@", self.pnum, self.ver];
        } else if (self.index == 0) {
            URL = [NSString stringWithFormat:@"http://api.huapipi.com/painting/original?deviceId=1826162&pnum=%ld&uid=1033268&entrance=recommend&ver=%@", self.pnum, self.ver];
        } else if (self.index == 2) {
            URL = [NSString stringWithFormat:@"http://api.huapipi.com/painting/original?deviceId=1826162&pnum=%ld&uid=1033268&entrance=latest&ver=%@", self.pnum, self.ver];
        }
        
        [ToolNetWorking ToolNetWorking:URL block:^(id result) {
            
            NSMutableArray *tempArr = [TableViewControllerModel BaseModel:result[@"rows"]];
            [self.arr addObjectsFromArray:tempArr];
            
            // 拼接网址刷新
            HotModel *tempModel = self.arr.lastObject;
            self.ver = tempModel.ver;
            
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
        }];
        
    }else {
        self.pnum = 1;
        if (self.index == 1) {
            
            [ToolNetWorking ToolNetWorking:@"http://api.huapipi.com/painting/original?deviceId=1826162&pnum=1&uid=1033268&entrance=hot" block:^(id result) {
                
                self.arr = [NSMutableArray array];
                self.arr = [TableViewControllerModel BaseModel:result[@"rows"]];
                
                // 拼接网址刷新
                HotModel *tempModel = self.arr.lastObject;
                self.ver = tempModel.ver;
                
                [self.tableView reloadData];
                [self.tableView.header endRefreshing];
            }];
        } else if (self.index == 0) {
            [ToolNetWorking ToolNetWorking:@"http://api.huapipi.com/painting/original?deviceId=1826162&pnum=1&uid=1033268&entrance=recommend" block:^(id result) {
                
                self.arr = [NSMutableArray array];
                self.arr = [TableViewControllerModel BaseModel:result[@"rows"]];
                
                // 拼接网址刷新
                HotModel *tempModel = self.arr.lastObject;
                self.ver = tempModel.ver;
                
                [self.tableView reloadData];
                [self.tableView.header endRefreshing];
            }];
        } else if (self.index == 2) {
            [ToolNetWorking ToolNetWorking:@"http://api.huapipi.com/painting/original?deviceId=1826162&pnum=1&uid=1033268&entrance=latest" block:^(id result) {
                
                self.arr = [NSMutableArray array];
                self.arr = [TableViewControllerModel BaseModel:result[@"rows"]];
                
                // 拼接网址刷新
                HotModel *tempModel = self.arr.lastObject;
                self.ver = tempModel.ver;
                [self.tableView reloadData];
                [self.tableView.header endRefreshing];
            }];
        }
    }
}



- (void)whichPage:(UISegmentedControl *)seg {
    if (seg.selectedSegmentIndex == 0) {
        self.index = 0;
        [self refreshAction];
        
    }else if (seg.selectedSegmentIndex == 1) {
        self.index = 1;
        [self refreshAction];
        
    }else {
        self.index = 2;
        [self refreshAction];
        
    }
    [self.tableView setContentOffset:CGPointMake(0, -64) animated:NO
     ];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
